<template>
  <div class="graph-table-list-wrap">
    <el-row>
      <div class="col col-1">
        <el-form label-width="120px" v-model="formData">
          <el-row class="alarm-detail">
            <el-row class="formRow">
              <el-col :span="sliderStyle.setNum" v-for="(item, index) in formData" :key="index">
                <span @click="colClick(item)">
                  <span :style="labelStyle">{{item.key}}</span>
                  <span v-if="delimiterConfig.show" :style="delimiterStyle">：</span>
                  <span
                    :style="contentStyle"
                    :class="delimiterConfig.show ? '' : 'delimiterCol'"
                  >{{item.value}}</span>
                </span>
              </el-col>
            </el-row>
          </el-row>
        </el-form>
      </div>
    </el-row>
  </div>
</template>

<script>
export default {
  name: "SingleTable",
  props: {
    option: {
      type: Object,
      default: () => ({
        tableField: [],
        formData: [],
        config: {},
        style: {}
      })
    }
  },
  data() {
    return {
      defaultConfig: {
        slider: {
          setNum: 3
        },
        label: {
          position: "4px",
          height: 40,
          isBold: false
        },
        content: {
          position: "4px",
          height: 40,
          isBold: false
        },
        delimiter: {
          show: true,
          size: 14,
          backgroundSize: 40,
          position: "4px",
          type: "icon"
        }
      },
      defaultStyle: {
        label: {
          color: "#606266",
          fontSize: "14px"
        },
        content: {
          color: "#303133",
          fontSize: "14px"
        },
        delimiter: {
          color: "#606266",
          fontSize: "14px"
        }
      }
    };
  },
  computed: {
    tableField: function() {
      let tableField = this.option?.tableField || [];
      if (tableField && tableField instanceof Array) {
        return tableField.reduce((r, c) => {
          r.push({
            key: c.key,
            prop: c.fieldName,
            label: c.fieldDescribe
          });
          return r;
        }, []);
      } else {
        return [];
      }
    },
    formData: function() {
      let formData = this.option?.formData || [];
      if (formData && formData instanceof Array) {
        return formData;
      } else {
        return [];
      }
    },
    // 配置项
    config: function() {
      return this.option?.config || this.defaultConfig;
      // return this.defaultConfig
    },
    // 样式配置
    style: function() {
      return this.option?.style || this.defaultStyle;
      // return this.defaultStyle
    },
    delimiterConfig: function() {
      return this.config.delimiter;
    },
    labelStyle: function() {
      return {
        ...this.style.label,
        height: this.config.label.height + "px",
        lineHeight: this.config.label.height + "px"
      };
    },
    contentStyle: function() {
      return {
        ...this.style.content,
        height: this.config.content.height + "px",
        lineHeight: this.config.content.height + "px"
      };
    },
    delimiterStyle: function() {
      return {
        ...this.style.delimiter,
        height: this.config.delimiter.height + "px",
        lineHeight: this.config.delimiter.height + "px"
      };
    },
    sliderStyle: function() {
      let config = {
        ...this.config.slider
      };
      if (config.setNum === 1) {
        config.setNum = 24;
      }
      if (config.setNum === 2) {
        config.setNum = 12;
      }
      if (config.setNum === 3) {
        config.setNum = 8;
      }
      if (config.setNum === 4) {
        config.setNum = 6;
      }
      return config;
    }
  },
  methods: {
    colClick(item) {
      const data = {
        label: item.value,
        value: item.value
      };
      console.log("单行表格点击事件: ", data);
      this.$emit("chartClick", data);
    }
  }
};
</script>

<style scoped lang="scss">
.alarm-public {
  width: 100%;
  height: 100%;
  background: #fff;
}
.col-1 {
  background: #fff;
  padding-left: 12px;
  padding-right: 12px;
  border-radius: 6px;
  margin-top: 25px;
  ::v-deep .el-form-item__content {
    word-break: break-all;
  }

  .alarm-detail {
    border: 1px #dcdfe6 solid;
    margin: 0 auto;
    margin-top: 25px;
    width: 100%;
    ::v-deep .el-form-item--small.el-form-item {
      margin-bottom: 6px;
      margin-top: 6px;
    }
  }
  .alarm-title {
    background: #f5f7fa;
    padding: 0 10px;
  }
}
.row-division-18 {
  height: 12px;
  width: 100%;
  background: #eee;
}
.formRow {
  padding: 0 20px;
  cursor: pointer;
}
.delimiterCol {
  margin-left: 14px;
}
</style>
